package org.datastealer.dao.mysql;

import org.datastealer.bean.Schema;
import org.datastealer.bean.mysql.MysqlColumn;
import org.datastealer.bean.mysql.MysqlTable;
import org.datastealer.dao.api.TableDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：mysql数据库TableDAO实现
 * @date ：Created in 2020/9/13
 */
@Component("tableDao")
public class MysqlTableDao implements TableDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    /**
     * 查询schema下的的全部表的sql
     */
    @Value("${datastealer.mysql.schema.table.query}")
    private String queryTableSql;
    /**
     * 查询表的ddl的sql
     */
    @Value("${datastealer.mysql.schema.table.ddl}")
    private String queryTableDDLSql;
    /**
     * 查询表的字段的sql
     */
    @Value("${datastealer.mysql.schema.table.column}")
    private String queryTableColumnSql;
    /**
     * 查询表的数据的sql
     */
    @Value("${datastealer.mysql.schema.table.data}")
    private String queryTableDataSql;

    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public List getTablesBySchema(String schemaName) {
        Schema schema = Schema.builder().schemaName(schemaName).build();
        SqlParameterSource paramSource = new BeanPropertySqlParameterSource(schema);
        return namedParameterJdbcTemplate.query(queryTableSql, paramSource, new BeanPropertyRowMapper<>(MysqlTable.class));
    }

    @Override
    public String getTableDDL(String schemaName, String tableName) {
        String sql = queryTableDDLSql.replace("schemaName", tableName).replace("$tableName", tableName);
        Map<String, Object> map = jdbcTemplate.queryForMap(sql);
        return map.getOrDefault("Create Table", "").toString();
    }

    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public List getTableColumns(String schemaName, String tableName) {
        Map<String, String> params = new HashMap<>(4);
        params.put("schemaName", schemaName);
        params.put("tableName", tableName);
        return namedParameterJdbcTemplate.query(queryTableColumnSql, params, new BeanPropertyRowMapper<>(MysqlColumn.class));
    }

    @Override
    public List<Map<String, Object>> getDataByPage(String schemaName, String tableName, int startRow, int endRow) {
        String sql = queryTableDataSql.replace("schemaName", tableName).replace("$tableName", tableName);
        Map<String, Object> params = new HashMap<>(4);
        params.put("startRow", startRow);
        params.put("endRow", endRow);
        return namedParameterJdbcTemplate.queryForList(sql, params);
    }

}